package com.hc.kotlin

fun main() {
    OutClass.InClass("zs").f2()
    var p1 = Point(-1,1)
    println(p1)
    //kotlin中==相当于java的equals,===相当于java的引用比较
    //Any的equals默认实现是引用比较
    var o1 =OutClass()
    var o2 =OutClass()
    var p2 = Point(-1,1)
    println(o1==o2)  //false
    println(o1===o2) //false
    println(p1==p2) //true 数据类默认重写了Any的equals和hashcode
    println(p1===p2) //false
    var p3 = p2.copy(-2,2) //走主构造函数
    println(p3)
    var(x,y) = Score(10,20)
    println(x+y)
    println(p1+p2)
}
class OutClass{
    class InClass(var name:String){ //嵌套类 存在于某类内部
        fun f2(){
            println(name)
        }
    }
    fun f1(){
        println("Hello")
    }
}
data class Point(var x:Int,var y:Int){ //数据类 专门存放数据 类似javabean
    var isvaild = x > 0 && y > 0
    operator fun plus(p:Point) = Point(x+p.x,y+p.y) //运算符重载
}
enum class Direct(){ //枚举类
    UP,DOWN,LEFT,RIGHT
}
enum class Direct1(var a:Point){
    Up(Point(1,1)),DOWN(Point(2,2)),LEFT(Point(3,3)),RIGHT(Point(4,4));
    fun update(p:Point) = Point(a.x+p.x,a.y+p.y) //枚举类也可以写方法 较少见
}
class Score(var s1:Int,var s2:Int){
    operator fun component1() = s1 //让属性支持解构
    operator fun component2() = s2
}
class DirectStatus(var s:Direct){
    fun check():String{
        return when(s){
            Direct.UP->"上"
            Direct.DOWN->"下"
            Direct.LEFT->"左"
            Direct.RIGHT->"右"
        }
    }
}
sealed class SealedDirect{ //密封类 枚举类的扩展
    object up:SealedDirect()
    class left(var i:String):SealedDirect()
}
class DirectStatus2(var s:SealedDirect){
    fun check():String{
        return when(s){
            is SealedDirect.up->"上上"
            is SealedDirect.left->"左左${(s as SealedDirect.left).i}"
        }
    }
}